package com.zjw.concurrency.aqs;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


/**
 * 模拟火箭升空倒计时，倒数计时结束，火箭发射
 * */
public class CountDownLatchExample {

    private static CountDownLatch countDownLatch = new CountDownLatch(5);

    public static void main(String[] args) throws InterruptedException {

        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            Thread thread = new Thread(new Runnable() {
               @Override
               public void run() {

                   int no = 5 - finalI;
                   System.out.println("发射倒数计时" + no +"秒");
                   //调用countdown()方法，当减到为0时，执行await()后面的内容
                   countDownLatch.countDown();
               }
           });
            executorService.submit(thread);
        }

        countDownLatch.await();
        System.out.println("倒计时结束，火箭发射");
        executorService.shutdown();
    }
}
